package com.yunhe.assist.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.assist.domain.Feedback;
import com.yunhe.assist.domain.FeedbackReply;
import com.yunhe.assist.enums.EnumFeedbackUserType;
import com.yunhe.assist.enums.EnumReplyStat;
import com.yunhe.assist.model.FeedbackMobileVO;
import com.yunhe.assist.repository.FeedbackRepository;
import com.yunhe.assist.service.FeedbackReplyService;
import com.yunhe.assist.service.FeedbackService;
import com.yunhe.assist.service.HelpService;
import com.yunhe.common.model.mybatis.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @description feedback
 * @author dlz
 * @date 2021-11-09
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackRepository feedbackRepository;
    @Autowired
    private FeedbackReplyService replyService;
    @Autowired
    private  HelpService helpService;


    @Override
    public int createFeedback (Feedback feedback, Long userId) {
        Assert.isTrue(StrUtil.isNotBlank(feedback.getUserType()), "用户类型不能为空");
        Assert.isTrue(StrUtil.isNotBlank(feedback.getContent()), "反馈内容不能为空");
        Assert.isTrue(StrUtil.isNotBlank(feedback.getPerson()), "用户姓名不能为空");
        Assert.isTrue(StrUtil.isNotBlank(feedback.getAccount()), "用户账号不能为空");
        if(feedback.getUserType().equals(EnumFeedbackUserType.COMPANY.getKey())){ //小程序端 企业反馈
            Assert.isTrue(StrUtil.isNotBlank(feedback.getPark()), "园区名称不能为空");
            Assert.isTrue(StrUtil.isNotBlank(feedback.getCompany()), "企业名称不能为空");
            Assert.isTrue(Objects.nonNull(feedback.getUserId()), "用户id不能为空");
        } else { //app端 园区反馈
            feedback.setUserId(userId);
        }

        //查询反馈集团 基于realStation查询
        helpService.handleFirmByStationId(feedback.getVirtualStationId(), map->{
            feedback.setProjectId(Long.valueOf(map.get("id").toString()));
            feedback.setProjectName(String.valueOf(map.get("enterpriseFullName")));
        });

        feedback.setCreateTime(new Date());
        feedback.setReplyStat(EnumReplyStat.NO.getKey());
        return feedbackRepository.createFeedback(feedback);
    }


    @Override
    public int deleteFeedback(int id) {
        return feedbackRepository.deleteFeedbackByPrimaryKey(id);
    }


    @Override
    public int updateFeedback(Feedback feedback) {
        return feedbackRepository.updateFeedbackByPrimaryKeySelective(feedback);
    }


    @Override
    public Feedback findFeedbackById(long id) {
        return feedbackRepository.searchFeedbackByPrimaryKey(id);
    }

    @Override
    public PageResult<Feedback> listForPark(Feedback feedback, int page, int size){
        //查询主表记录
        PageHelper.startPage(page, size);
        List<Feedback> list = feedbackRepository.findList(feedback);
        PageInfo<Feedback> pageInfo = new PageInfo<>(list);
        return new PageResult(page, size, (int)pageInfo.getTotal(), list);
    }

    @Override
    public PageResult<FeedbackMobileVO> listForUser(Feedback feedback, int page, int size) {
        //查询主表记录
        PageHelper.startPage(page, size);
        List<Feedback> list = feedbackRepository.findList(feedback);
        PageInfo<Feedback> pageInfo = new PageInfo<>(list);

        //数据合并
        List<FeedbackMobileVO> voList = new ArrayList<>();
        list.forEach(f->{
            FeedbackMobileVO vo = new FeedbackMobileVO();
            vo.setFeedback(f);
            vo.setFeedbackReply(replyService.getFirstByFeedbackId(f.getId()));
            voList.add(vo);
        });

        return new PageResult(page, size, (int)pageInfo.getTotal(), voList);
    }

    @Override
    public JSONObject detail(long id) {
        //查询主表信息
        Feedback feedback = this.findFeedbackById(id);
        if(Objects.isNull(feedback)){
            return new JSONObject(){{
               put("feedback", new Feedback());
               put("replyList", new ArrayList<>(0));
            }};
        }

        //查询回复列表
        List<FeedbackReply> replyList = replyService.findListByFeedbackId(id);
        return new JSONObject(){{
            put("feedback", feedback);
            put("replyList", replyList);
        }};
    }

    @Override
    public void updateReplyStat(long feedbackId, int stat) {
        Feedback feedback = new Feedback(){{
            setId(feedbackId);
            setReplyStat(stat);
        }};
        this.updateFeedback(feedback);
    }


}